package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.PersonalDataCardBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Jinni on 2017/8/3 0003.
 */

public class PersonalDataCardModel implements PersonalDataCardModelImpl {

    private static final String TAG = "PersonalDataCardModel";
    private OnRequestListener mListener;

    public PersonalDataCardModel(OnRequestListener listener) {
        mListener = listener;
    }

    @Override
    public void getPersonalInfoBean(String uid) {
        Observable<PersonalDataCardBean> loginResult = DataManager.getPersonalCardInfoResult(
                uid
        );
        Observable<PersonalDataCardBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<PersonalDataCardBean>() {

            @Override
            public void onNext(@NonNull PersonalDataCardBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void isFriend(String fUid, String tUid) {
        Observable<CommonBean> loginResult = DataManager.isFriend(
                fUid,
                tUid
        );
        Observable<CommonBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                if (mListener != null) {
                    mListener.isFriend(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getDelFriendRequestResult(String uid, String toUid) {
        Observable<CommonBean> loginResult = DataManager.getDelFriendRequestResult(
                uid,
                toUid
        );
        Observable<CommonBean> signUpBeanObservable = loginResult.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        signUpBeanObservable.subscribe(new DisposableObserver<CommonBean>() {

            @Override
            public void onNext(@NonNull CommonBean bean) {
                if (mListener != null) {
                    mListener.onDelSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestListener{
        void onSuccess(PersonalDataCardBean bean);
        void onDelSuccess(CommonBean bean);
        void isFriend(CommonBean bean);
        void onError(Throwable e);
    }
}
